package com.guigu.eduservice.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.guigu.eduservice.entity.SellerQualification;
import com.guigu.eduservice.entity.User;
import com.guigu.eduservice.mapper.SellerQualificationMapper;
import com.guigu.eduservice.mapper.UserMapper;
import com.guigu.eduservice.service.SellerQualificationService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.guigu.eduservice.vo.SellerVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.List;

/**
 * <p>
 * 卖家资质表（存储卖家实名认证材料及审核结果） 服务实现类
 * </p>
 *
 * @author janecraft
 * @since 2025-09-27
 */
@Service
public class SellerQualificationServiceImpl extends ServiceImpl<SellerQualificationMapper, SellerQualification> implements SellerQualificationService {

    @Autowired
    private SellerQualificationMapper sellerQualificationMapper;
    @Autowired
    private UserMapper userMapper;
    @Override
    public PageInfo<SellerQualification> page(Integer pageNo, Integer pageSize, SellerVo vo) {
        if (pageNo == null || pageNo < 1) {
            pageNo = 1; // 默认查询第1页
        }
        if (pageSize == null || pageSize < 1 || pageSize > 100) {
            pageSize = 10; // 默认每页10条，最大限制100条（避免性能问题）
        }

        PageHelper.startPage(pageNo, pageSize);

        LambdaQueryWrapper<SellerQualification> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        if (vo != null){
            if (vo.getAuditStatus() != null){
                lambdaQueryWrapper.eq(SellerQualification::getAuditStatus, vo.getAuditStatus());
            }
        }
        List<SellerQualification> sellerQualificationList = sellerQualificationMapper.selectList(lambdaQueryWrapper);
        return new PageInfo<>(sellerQualificationList);
    }

    @Override
    public boolean updateAudit(SellerVo vo) {
        SellerQualification sellerQualification = sellerQualificationMapper.selectById(vo.getQualificationId());
        sellerQualification.setAuditStatus(vo.getAuditStatus());
        sellerQualification.setAuditRemark(vo.getAuditRemark());
        sellerQualification.setAuditTime(LocalDateTime.now());
        int i = sellerQualificationMapper.updateById(sellerQualification);
        User user = userMapper.selectById(vo.getUserId());
        user.setUserType("1");
        userMapper.updateById(user);
        return i > 0;
    }
}
